<?php 
include_once 'inc/config.inc.php';
include_once 'inc/mysql.inc.php';
include_once 'inc/tool.inc.php';
include_once 'inc/page.inc.php';
$link=connect();
$member_id=is_login($link);

if(!isset($_GET['id']) || !is_numeric($_GET['id'])){
	skip('index.php', 'error', '帖子id参数不合法!');
}
// 输出帖子详细内容
$query="select sc.id, sc.module_id, sc.title, sc.content, sc.time, sc.member_id, sc.times, sm.name, sm.photo 
        from sfk_content sc,sfk_member sm 
        where sc.id={$_GET['id']} and sc.member_id=sm.id " ;
$result_content=execute($link,$query);
if(mysqli_num_rows($result_content)!=1){
	skip('index.php', 'error', '本帖子不存在!');
}
$data_content=mysqli_fetch_assoc($result_content);
// 将数据库的内容原封不动的输出
$data_content['title']=htmlspecialchars($data_content['title']);
$data_content['content']=nl2br(htmlspecialchars($data_content['content']));
$query="select * from sfk_son_module where id={$data_content['module_id']}";
$result_son=execute($link,$query);
$data_son=mysqli_fetch_assoc($result_son);

$query="select * from sfk_father_module where id={$data_son['father_module_id']}";
$result_father=execute($link,$query);
$data_father=mysqli_fetch_assoc($result_father);

$query = "SELECT count(id) from sfk_reply WHERE content_id = {$_GET['id']}";
$row = execute($link,$query)->fetch_array();

$template['title']='帖子详细页';
$template['css']=array('style/public.css','style/show.css');

// 分页
$number = 5;
$query = "select count(*) from sfk_reply where content_id={$_GET['id']}";
$count_reply = num($link, $query);
$page = page($count_reply, $number);

// 回复信息
$query = "SELECT r.id, r.content_id, r.quote_id, r.content, r.member_id, r.time, m.name, m.photo from sfk_reply r LEFT JOIN sfk_member m ON r.member_id = m.id  where r.content_id = {$_GET['id']} and r.quote_reply = 0 ";
$datas = execute($link, $query.$page['limit']);
$i = $_GET['page']*$number-4;
?>
<?php include 'inc/header.inc.php'?>
<div id="position" class="auto">
    <a href="index.php">首页</a> &gt;
    <a href="list_father.php?id=<?php echo $data_father['id']?>">
    <?php echo $data_father['module_name']?></a> &gt;
    <a href="list_son.php?id=<?php echo $data_son['id']?>">
    <?php echo $data_son['module_name']?></a> &gt;
    <?php echo $data_content['title']?>

</div>
<div id="main" class="auto">
	<div class="wrap1">
        <div class="pages">
            <?=$page['html']?>
        </div>
		<a class="btn reply" href="reply.php?id=<?=$_GET['id']?>" target="_blank"></a>
		<div style="clear:both;"></div>
	</div>
	<div class="wrapContent">
		<div class="left">
			<div class="face">
				<a target="_blank" href="#">
					<img width=120 height=120 src="<?php if($data_content['photo']!=''){echo $data_content['photo'];}else{echo 'style/photo.jpg';}?>" />
				</a>
			</div>
			<div class="name">
				<a href="member.php?id=<?=$data_content['member_id']?>"><?php echo $data_content['name']?></a>
			</div>
		</div>
		<div class="right">
			<div class="title">
				<h2><?php echo $data_content['title']?></h2>
				<span>阅读：<?php echo $data_content['times']?>&nbsp;|&nbsp;回复：<?=$row[0]?></span>
				<div style="clear:both;"></div>
			</div>
			<div class="pubdate">
				<span class="date">发布于：<?php echo $data_content['time']?> </span>
				<span class="floor" style="color:red;font-size:14px;font-weight:bold;">楼主</span>
			</div>
			<div class="content">
				 <?php echo $data_content['content']?>
			</div>
		</div>
		<div style="clear:both;"></div>
	</div>
    <?php
    while ($data = $datas->fetch_array()) {
        $data['title']=htmlspecialchars($data['title']);
        $data['content']=nl2br(htmlspecialchars($data['content']));
    ?>
<!--    回复楼层-->
	<div class="wrapContent">
		<div class="left">
			<div class="face">
                <a target="_blank" href="">
                    <img width=120 height=120 src="<?php if($data['photo']!=''){echo $data['photo'];}else{echo 'style/photo.jpg';}?>" />
                </a>
			</div>
			<div class="name">
				<a href="member.php?id=<?=$data['member_id']?>"><?=$data['name']?></a>
			</div>
		</div>

		<div class="right">
			<div class="pubdate">
				<span class="date">回复时间：<?=$data['time']?></span>
				<span class="floor"><?=$i++?>楼&nbsp;| &nbsp;<a href="quote.php?id=<?=$_GET['id']?>&reply_id=<?=$data['id']?>" >引用</a></span>
			</div>
			<div class="content">
				<?=$data['content']?>
			</div>
		</div>
        <?php
        if ($data['quote_id'] != 0) {
            $query = "SELECT r.id, r.content_id, r.quote_id, r.member_id, r.content, r.time, m.name from sfk_reply r LEFT JOIN sfk_member m ON r.member_id = m.id where r.id = {$data['quote_id']}";
            $quote_content = execute($link,$query);
            while ( $da = $quote_content->fetch_array()) {
        ?>
        <div class="right"  style="background-color: #e2d8d8; border-top: solid 1px gray">
            <div class="pubdate">
                <span class="date"><b>
                        <a href="member.php?id=<?=$da['member_id']?>">
                            <?=$da['name']?>
                        </a>@<a href="member.php?id=<?=$data['member_id']?>">
                            <?=$data['name']?>
                        </a>
                    </b>
                    回复时间：<?=$da['time']?></span>
                <span class="floor"><a href="quote.php?id=<?=$_GET['id']?>&reply_id=<?=$da['id']?>" >引用</a></span>
            </div>
            <div class="content">
                <?=$da['content']?>
            </div>
        </div>

        <?php
            }
        }
        ?>
		<div style="clear:both;"></div>
	</div>
    <?php
        }
    ?>
    <div class="wrap1">
        <div class="pages">
            <?php
            echo $page['html'];
            ?>
        </div>
        <a class="btn reply" href="reply.php?id=<?php echo $_GET['id']?>" target="_blank"></a>
        <div style="clear:both;"></div>
    </div>
</div>
<?php include 'inc/footer.inc.php'?>